﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OdeToFoo.Models;

namespace OdeToFoo.Controllers
{
    public class HomeController : Controller
    {

        OdeToFooDb _db = new OdeToFooDb();

        public ActionResult Index(string searchTerm = null)
        {

            //var controller = RouteData.Values["controller"];
            //var action = RouteData.Values["action"];
            //var id = RouteData.Values["id"];
            //var message = String.Format("{0}::{1} {2}", controller, action, id);
            //ViewBag.Message = message;
            
            var model = from r in _db.Restaurants.ToList()
                        orderby r.Name descending
                        where searchTerm == null ||
                        r.Name.ToLowerInvariant().StartsWith(searchTerm.ToLowerInvariant())
                        select r;
            
            
            
            return View(model);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            var m = new AboutModel();
            m.Location = "Beijing, China";
            m.Name = "Robin Ren";

            return View(m);
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        protected override void Dispose(bool disposing)
        {
            if (_db != null)
            {
                _db.Dispose();
            }
            
            base.Dispose(disposing);
        }
    }
}
